<template>
    <div>
       <el-breadcrumb separator-class="el-icon-arrow-right">
         <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
         <el-breadcrumb-item>演员管理</el-breadcrumb-item>
         <el-breadcrumb-item>新增演员</el-breadcrumb-item>
     </el-breadcrumb>
     <el-divider></el-divider>
    <el-form ref="form" :model="form" label-width="80px" :rules="rules">
 <el-form-item label="演员头像" prop="actorAvatar">
 <el-upload
  class="avatar-uploader"
  action="http://localhost:9000/upload"
  :on-success="handleAvatarSuccess"
  :before-upload="beforeAvatarUpload">
  <el-image v-if="form.actorAvatar" :src="form.actorAvatar" class="avatar" fit='cover' />
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</el-form-item>
        <el-form-item label="演员姓名" prop="actorName">
            <el-input v-model="form.actorName" style="width:300px;"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="Insert">立即新增</el-button>
          <el-button @click="reset('form')">取消</el-button>
        </el-form-item>
   </el-form>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                form:{  //绑定表单数据
                 actorName:'',
                 actorAvatar:''
                },
                 rules:{
                  actorName: [{ required: true, message: "请输入演员姓名", trigger: "blur" }],
                  actorAvatar:[{required:true,message:'请上传演员图片',trigger:'blur'}]
                }
            }
        },
        methods: {
            //添加按钮
            Insert(){
           this.$refs['form'].validate((valid) => {
          if (valid) {
             this.$http.actorApi.add(this.form).then(res => {
                console.log(res)
                if(res.data.code == 200){
                    this.$alert('添加成功', '新增演员', {
                    confirmButtonText: '确定', 
                }).then(()=>{
                    this.$router.push('/home/actor-list')
                })
                }
              })
          } else {
             this.$message({
            type: 'warning',
            message: '请完善表单字段'
          });
          }
        }); 
      },
      //重置表单
      reset(form){
          this.$refs[form].resetFields();
      },
            /**
             * 上传成功后 回显图片
             */
             handleAvatarSuccess(res, file) {
                console.log(res)
               if(res.code == 200){
                this.form.actorAvatar = res.data
               }
            },
            /**
             * 处理上传之前的图片
             */
             beforeAvatarUpload(file) {
               const isJPG = file.type === 'image/jpeg';
               const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                   this.$message.error('上传头像图片只能是 JPG 格式!');
            }
                if (!isLt2M) {
                   this.$message.error('上传头像图片大小不能超过 2MB!');
            }
               return isJPG && isLt2M;
         }
       }
    }
</script>

<style>
   .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>